package com.shd.edanpei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.UpFileAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.PicBean;
import com.shd.edanpei.bean.UpFileBean;
import com.shd.edanpei.presenter.contract.UpFileContract;
import com.shd.edanpei.enums.PicEnum;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.UpFilePresenter;
import com.shd.edanpei.util.GsonUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/4/4.
 * 上传证件
 */

public class UpFileActivity extends BaseActivity<UpFilePresenter> implements UpFileContract.UpFileIView {


    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    //上传图片类型
    private int picTypeID;
    //上传图片组件
    List<UpFileBean> list = new ArrayList<>();

    private UpFileAdapter mAdapter;
    private PicBean picBean;

    private List<String> imgList = new ArrayList<>();
    private List<String> urlList;

    private final Type type = new TypeToken<List<String>>() {
    }.getType();

    @Override
    protected int setLayout() {
        return R.layout.activity_upfile;
    }

    @Override
    protected UpFilePresenter bindPresenter() {
        return new UpFilePresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("上传文件");
        loadImageCtrl = new LoadImageCtrl(this);
        Bundle extras = getIntent().getExtras();
        picTypeID = extras.getInt(Constant.TYPE);
        picBean = extras.getParcelable(Constant.DATA);
        initData();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new UpFileAdapter(this, Constant.EDIT);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setNewData(list);

    }

    private void initData() {
//        if (picTypeID == PicEnum.idCardPic.getId()) {
//            urlList = GsonUtil.getGson().fromJson(picBean.getIdCardPic(), type);
//            list.add(new UpFileBean(true, "身份证正面"));
//            list.add(new UpFileBean(true, "身份证反面"));
//        } else
        tvHint.setText("提示：请上传" + PicEnum.getType(picTypeID) + "，信息仅用于系统审核");
        if (picTypeID == PicEnum.householdRegister.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getHouseholdRegister(), type);
            list.add(new UpFileBean(true, "点击拍摄户口本主页"));
            list.add(new UpFileBean(true, "点击拍摄户口本个人页"));
        } else if (picTypeID == PicEnum.roomCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getRoomCert(), type);
            list.add(new UpFileBean(true, "点击拍摄房产证/不动产证"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        } else if (picTypeID == PicEnum.businessCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getBusinessCert(), type);
            list.add(new UpFileBean(true, "点击拍摄营业执照"));
        } else if (picTypeID == PicEnum.otherHouseCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getOtherHouseCert(), type);
            list.add(new UpFileBean(true, "点击拍摄房产/不动产证"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        } else if (picTypeID == PicEnum.carCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getCarCert(), type);
            list.add(new UpFileBean(true, "点击拍摄车辆登记证"));
            list.add(new UpFileBean(true, "点击拍摄驾驶证"));
        } else if (picTypeID == PicEnum.carWarranty.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getCarWarranty(), type);
            list.add(new UpFileBean(true, "点击拍摄车辆保单"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        } else if (picTypeID == PicEnum.maritalCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getMaritalCert(), type);
            list.add(new UpFileBean(true, "点击拍摄结婚证/离婚证"));
        } else if (picTypeID == PicEnum.creditCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getCreditCert(), type);
            list.add(new UpFileBean(true, "点击拍摄征信"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        } else if (picTypeID == PicEnum.supplement.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getSupplement(), type);
            list.add(new UpFileBean(true, "点击拍摄补充信息"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        } else if (picTypeID == PicEnum.newCreditCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getCreditCert(), type);
            list.add(new UpFileBean(true, "点击拍摄点击拍摄征信"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        }else if (picTypeID == PicEnum.newBusinessCert.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getBusinessCert(), type);
            list.add(new UpFileBean(true, "点击拍摄营业执照"));
        } else if (picTypeID == PicEnum.newWarranty.getId()) {
            urlList = GsonUtil.getGson().fromJson(picBean.getCarWarranty(), type);
            list.add(new UpFileBean(true, "点击拍摄保单"));
            list.add(new UpFileBean(false));
            list.add(new UpFileBean(false));
        }

        if (urlList != null) {
            for (int i = 0; i < urlList.size(); i++) {
                list.get(i).setUrl(urlList.get(i));
            }
        }
    }


    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ChoosePicActivity.start(UpFileActivity.this, position);
            }
        });

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }


    @OnClick(R.id.tv_submit)
    void submit() {
        mPresenter.submit(picTypeID);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ChoosePicActivity.INTENT_REQUEST_CODE) {
            if (null != data) {
                String picPath = data.getStringExtra(ChoosePicActivity.INTENT_BACK_DATA);
                mPresenter.upFile(picPath, resultCode);
            }
        }
    }


    @Override
    public UpFileAdapter getAdapter() {
        return mAdapter;
    }

}
